package es.ats.codington.spring.controllers;

import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.services.EventService;
import es.ats.codington.festival.services.VisitorService;



/**
 * The Class SignUpEventController.
 */
@Controller
public class SignUpEventController {
	
	/**
	 * Register event controller.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the model and view
	 */
	@RequestMapping(value = "/signup_event.htm")
	public ModelAndView registerEventController(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView modelAndView=new ModelAndView();
		
		Object iD = request.getSession().getAttribute("iDVisitor");
	    int id= (Integer) iD;
	    int idevent = Integer.parseInt(request.getParameter("idevent"));
	    String msg=null;
		try{
		
    	VisitorService vs = new VisitorService();
    	boolean tt = vs.signUpEvent(id, idevent); 

    	if(	tt==true){

		 EventService es = new EventService();
			ArrayList<Event> myevents = new ArrayList<Event>();
			ArrayList<Integer> myidevents = es.viewMyEvent(id);
			int size = myidevents.size();
			modelAndView.addObject("size", size);

			for (int iter=0; iter<size; iter++){
				Event event = es.viewEvent(myidevents.get(iter));
				myevents.add(event);			
			}
			modelAndView.addObject("evento", myevents);
			
    		modelAndView.setViewName("viewmyevents");
 
	    	}else{
	    		modelAndView.setViewName("viewmyevents");
	    	
    	}
	    }catch(Exception ex){
	    	msg= ex.getMessage();
	    	modelAndView.addObject("msg",msg);
			
			modelAndView.setViewName("redirect:/show_event.htm?idevent="+idevent);
			return modelAndView;
			
			
	    }
	    return modelAndView;
	    }
	

	/**
	 * Un register event controller.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the model and view
	 */
	@RequestMapping(value = "/unsignup_event.htm")
	public ModelAndView unRegisterEventController(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView modelAndView=new ModelAndView();
		
		Object iD = request.getSession().getAttribute("iDVisitor");
	    int id= (Integer) iD;
	    int idevent = Integer.parseInt(request.getParameter("idevent"));
	    
	    try{
			
	    	VisitorService vs = new VisitorService();
	    	boolean tt = vs.unsignUpEvent(id, idevent); 

	    	if(	tt==true){

			 EventService es = new EventService();
				ArrayList<Event> myevents = new ArrayList<Event>();
				ArrayList<Integer> myidevents = es.viewMyEvent(id);
				int size = myidevents.size();
				modelAndView.addObject("size", size);

				for (int iter=0; iter<size; iter++){
					Event event = es.viewEvent(myidevents.get(iter));
					myevents.add(event);			
				}
				modelAndView.addObject("evento", myevents);
				
	    		modelAndView.setViewName("viewmyevents");
	    	}else{
	    		modelAndView.setViewName("viewmyevents");
	    	}
	    }catch(Exception ex){
	    	ex.getMessage();
	    	modelAndView.setViewName("mainpage");
	    }
	    return modelAndView;
	}
	
}
